﻿Imports System.IO
Imports System.Text

''' <summary>
''' Classe responsavel por manipulacao de arquivos dentro de um diretório
''' </summary>
''' <remarks></remarks>
Public Class FileUtil

#Region "Declaração de métodos publicos"
    Public Shared Sub ProtectedFile(ByVal strFileName As String)
        File.SetAttributes(strFileName, FileAttributes.Archive Or FileAttributes.ReadOnly Or FileAttributes.Hidden)
    End Sub

    Public Shared Sub DesprotectedFile(ByVal strFileName As String)
        If ((FileIsReadOnly(strFileName)) OrElse (FileIsHidden(strFileName))) Then
            File.SetAttributes(strFileName, FileAttributes.Archive)
        End If
    End Sub

    Public Shared Function ExistsFile(ByVal strFileName As String) As Boolean
        Return (File.Exists(strFileName))
    End Function

    Public Shared Function IsDirectory(ByVal strFileName As String) As Boolean
        Dim fli As New FileInfo(strFileName)
        Return fli.Attributes = FileAttributes.Directory
    End Function

    Public Shared Sub DeleteFile(ByVal strFileName As String)
        If (ExistsFile(strFileName)) Then
            Try
                File.Delete(strFileName)
            Catch ex As Exception
                'Ignora, o arquivo pode estar na memoria.
            End Try
        End If
    End Sub

    Public Shared Function FileServerIsNew(ByVal strFileServidor As String, ByVal strFileCliente As String) As Boolean
        If Not FileUtil.IsDirectory(strFileServidor) Then
            If Not ExistsFile(strFileCliente) Then
                Return True
            ElseIf File.GetLastWriteTime(strFileServidor) > File.GetLastWriteTime(strFileCliente) Then
                Return True
            End If
            Return False
        End If
        Return False
    End Function

#End Region

#Region "Metodos privados"

    Private Shared Function FileIsReadOnly(ByVal strFileName As String) As Boolean
        If (ExistsFile(strFileName)) Then
            Dim objAttributes As FileAttributes = File.GetAttributes(strFileName)
            If ((objAttributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly) Then
                Return (True)
            End If
        End If
        Return (False)

    End Function

    Private Shared Function FileIsHidden(ByVal strFileName As String) As Boolean
        If (ExistsFile(strFileName)) Then
            Dim objAttributes As FileAttributes = File.GetAttributes(strFileName)
            If ((objAttributes And FileAttributes.Hidden) = FileAttributes.Hidden) Then
                Return (True)
            End If
            Return (False)
        End If
    End Function
#End Region

End Class
